package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.dbs.Database;
import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.PayOrderInfoMapper;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.enums.TransStatusEnum;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/2/6
 */
public class PayOrderInfoMapperImpl implements PayOrderInfoMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public PayOrderInfoMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertPayOrderInfo(PayOrderInfo payOrderInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertPayOrderInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, payOrderInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记支付订单信息失败,instId:<{}>,sysSeqNum:<{}>,mchntOrderId:<{}>,msgDstId:<{}>,iReturnCode:<{}>",
                    payOrderInfo.getInstId(), payOrderInfo.getSysSeqNum(), payOrderInfo.getMchntOrderId(), payOrderInfo.getMsgDstId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updatePayOrderInfoByPrimaryKey(PayOrderInfo payOrderInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updatePayOrderInfoByPrimaryKey");
        payOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, payOrderInfo, null);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updatePayOrderInfoHisByPrimaryKey");
            iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, payOrderInfo, null);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("根据主键更新支付订单信息失败,instId:<{}>,sysSeqNum:<{}>,msgDstId:<{}>,dstCallIndex:<{}>,iReturnCode:<{}>",
                        payOrderInfo.getInstId(), payOrderInfo.getSysSeqNum(), payOrderInfo.getMsgDstId(), payOrderInfo.getDstCallIndex(), iReturnCode);
            }
        }
        return iReturnCode;
    }

    @Override
    public PayOrderInfo selectPayOrderInfoByPrimaryKey(String instId, String sysSeqNum, String... dstArrary) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderInfoByPrimaryKey");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setSysSeqNum(sysSeqNum);
        if (dstArrary.length > 1) {
            payOrderInfo.setMsgDstId(dstArrary[0]);
            payOrderInfo.setDstCallIndex(dstArrary[1]);
        } else {
            payOrderInfo.setMsgDstId(dstArrary[0]);
            payOrderInfo.setDstCallIndex("0");
        }
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, payOrderInfo, payOrderInfo);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            rglog.warn("根据主键查询支付订单信息失败,instId:<{}>,sysSeqNum:<{}>,msgDstId:<{}>,dstCallIndex:<{}>,iReturnCode:<{}>", instId, sysSeqNum, payOrderInfo.getMsgDstId(), payOrderInfo.getDstCallIndex(), iReturnCode);
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderInfoHisByPrimaryKey");
            iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, payOrderInfo, payOrderInfo);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("根据主键查询历史支付订单信息失败,instId:<{}>,sysSeqNum:<{}>,msgDstId:<{}>,dstCallIndex:<{}>,iReturnCode:<{}>", instId, sysSeqNum, payOrderInfo.getMsgDstId(), payOrderInfo.getDstCallIndex(), iReturnCode);
                return null;
            }
        }
        return payOrderInfo;
    }

    @Override
    public List<PayOrderInfo> selectPayOrderInfoBySysSeqNum(String instId, String sysSeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderInfoBySysSeqNum");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setSysSeqNum(sysSeqNum);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
        if (null != objectList && objectList.isEmpty()) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderInfoHisBySysSeqNum");
            objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
            if (null == objectList || objectList.isEmpty()) {
                rglog.warn("根据系统流水号查询支付订单信息失败,instId:<{}>,sysSeqNum:<{}>", instId, sysSeqNum);
                return null;
            }
        } else if (null == objectList) {
            rglog.warn("根据系统流水号查询支付订单信息失败,instId:<{}>,sysSeqNum:<{}>", instId, sysSeqNum);
            return null;
        }
        return objectList.stream().map(item -> (PayOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public PayOrderInfo selectPayOrderInfoByChannelSeqNum(String instId, String msgDstId, String channelSeqNum) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderInfoByChannelSeqNum");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setMsgDstId(msgDstId);
        payOrderInfo.setChannelSeqNum(channelSeqNum);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, payOrderInfo, payOrderInfo);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            rglog.warn("根据通道流水号查询支付订单信息失败,instId:<{}>,msgDstId:<{}>,channelSeqNum:<{}>,iReturnCode:<{}>", instId, msgDstId, channelSeqNum, iReturnCode);
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderInfoHisByChannelSeqNum");
            iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, payOrderInfo, payOrderInfo);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("根据通道流水号查询历史支付订单信息失败,instId:<{}>,msgDstId:<{}>,channelSeqNum:<{}>,iReturnCode:<{}>", instId, msgDstId, channelSeqNum, iReturnCode);
                return null;
            }
        }
        return payOrderInfo;
    }

    @Override
    public List<PayOrderInfo> selectPayOrderInfoByOrigSysSeqNum(String instId, String origSysSeqNum, String... dstArrary) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderInfoByOrigSysSeqNum");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setOrigSysSeqNum(origSysSeqNum);
        if (dstArrary.length > 1) {
            payOrderInfo.setMsgDstId(dstArrary[0]);
            payOrderInfo.setDstCallIndex(dstArrary[1]);
        } else if (dstArrary.length == 1) {
            payOrderInfo.setMsgDstId(dstArrary[0]);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
        if (null != objectList && objectList.isEmpty()) {
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderInfoHisByOrigSysSeqNum");
            objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
            if (null == objectList || objectList.isEmpty()) {
                rglog.warn("根据原交易流水号查询支付订单信息失败,instId:<{}>,origSysSeqNum:<{}>,msgDstId:<{}>,dstCallIndex:<{}>", instId, origSysSeqNum, payOrderInfo.getMsgDstId(), payOrderInfo.getDstCallIndex());
                return null;
            }
        } else if (null == objectList) {
            rglog.warn("根据原交易流水号查询支付订单信息失败,instId:<{}>,origSysSeqNum:<{}>,msgDstId:<{}>,dstCallIndex:<{}>", instId, origSysSeqNum, payOrderInfo.getMsgDstId(), payOrderInfo.getDstCallIndex());
            return null;
        }
        return objectList.stream().map(item -> (PayOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<PayOrderInfo> selectPayOrderInfoByInstIdMsgDstId(String instId, String msgDstId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderInfoByInstIdMsgDstId");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setMsgDstId(msgDstId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
        if (null != objectList && objectList.isEmpty()) {
            rglog.warn("根据系统流水号查询支付订单信息失败,instId:<{}>,msgDstId:<{}>", instId, msgDstId);
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderInfoHisByInstIdMsgDstId");
            objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
            if (null == objectList || objectList.isEmpty()) {
                rglog.error("根据系统流水号查询历史支付订单信息失败,instId:<{}>,msgDstId:<{}>", instId, msgDstId);
                return null;
            }
        } else if (null == objectList) {
            rglog.error("根据系统流水号查询历史支付订单信息失败,instId:<{}>,msgDstId:<{}>", instId, msgDstId);
            return null;
        }
        return objectList.stream().map(item -> (PayOrderInfo) item).collect(Collectors.toList());
    }

    @Override
    public PayOrderInfo selectPayOrderInfoByChannelReconKey(String instId, String msgDstId, String channelReconKey) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderInfoByChannelReconKey");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setMsgDstId(msgDstId);
        payOrderInfo.setChannelReconKey(channelReconKey);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, payOrderInfo, payOrderInfo);
        if (Database.DBS_NOT_FOUND == iReturnCode) {
            rglog.warn("根据通道对账唯一键值查询支付订单信息失败,instId:<{}>,msgDstId:<{}>,channelReconKey:<{}>,iReturnCode:<{}>", instId, msgDstId, channelReconKey, iReturnCode);
            sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderInfoHisByChannelReconKey");
            iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, payOrderInfo, payOrderInfo);
            if (Database.DBS_SUCCESS != iReturnCode) {
                rglog.error("根据通道对账唯一键值查询历史支付订单信息失败,instId:<{}>,msgDstId:<{}>,channelReconKey:<{}>,iReturnCode:<{}>", instId, msgDstId, channelReconKey, iReturnCode);
                return null;
            }
        }
        return payOrderInfo;
    }

    @Override
    public List<PayOrderInfo> selectPayOrderInfoByAcctNoAndAuthCode(String instId, String mchntNo, String txnNum, String payAcctNo, String authCode) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectPayOrderInfoByAcctNoAndAuthCode");
        PayOrderInfo payOrderInfo = new PayOrderInfo();
        payOrderInfo.setInstId(instId);
        payOrderInfo.setMchntNo(mchntNo);
        payOrderInfo.setTxnNum(txnNum);
        payOrderInfo.setPayerAcctInfo(payAcctNo);
        payOrderInfo.setTxnAuthCode(authCode);
        payOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, payOrderInfo, PayOrderInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据支付账号及授权码查询支付订单信息失败,instId:<{}>,payAcctNo:<{}>,authCode:<{}>", instId, payAcctNo, authCode);
            return null;
        }
        return objectList.stream().map(item -> (PayOrderInfo) item).collect(Collectors.toList());
    }
}